﻿//
// Copyright 2024 Exhunter Technology Co.Ltd2.
//

using System;
using System.Reflection;
using Newtonsoft.Json;

namespace X18.Framework.Core.Utils
{
    public static class ObjectUtils
    {
        /// <summary>
        /// 判断指定的对象是否是空引用，或空字符串。
        /// </summary>
        /// <param name="value">需要测试的对象</param>
        /// <returns>如果 value 参数为空引用或空字符串 ("")，则为 true；否则为 false。</returns>
        public static bool IsNullOrEmpty(this object value)
        {
            if (value == null)
            {
                return true;
            }
            else
            {
                string value0 = value.ToString();
                if (value0 == null || value0.Length == 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断指定的对象是否是空引用，或空字符串， 或空白字符。
        /// </summary>
        /// <param name="value">需要测试的对象</param>
        /// <returns>如果 value 参数为空引用或空字符串 ("")或空白字符，则为 true；否则为 false。</returns>
        public static bool IsNullOrEmptyOrWhiteSpace(this object value)
        {
            if (value == null)
            {
                return true;
            }
            else
            {
                string value0 = value.ToString();
                if (value0 == null || value0.Length == 0 || string.IsNullOrWhiteSpace(value0))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取对象的字符串值
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <returns>字符串</returns>
        public static string GetString(this object value)
        {
            string result = null;
            if (value != null)
            {
                result = value.ToString();
            }
            if (result == null)
            {
                result = string.Empty;
            }
            return result;
        }

        /// <summary>
        /// 得到对象对应的字符串
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <returns>字符串</returns>
        public static string ObjectToString(object value)
        {
            if (value != null)
            {
                string result = value.ToString();
                if (result == null)
                {
                    result = string.Empty;
                }
                return result;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 得到对象对应的布尔值
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>得到的布尔值</returns>
        public static bool ObjectToBool(object value, bool defaultValue = false)
        {
            if (value.IsNullOrEmptyOrWhiteSpace())
            {
                return defaultValue;
            }
            try
            {
                return Convert.ToBoolean(value);
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 得到对象对应的整数值
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>得到的整数值</returns>
        public static int ObjectToInt(object value, int defaultValue = 0)
        {
            if (value.IsNullOrEmptyOrWhiteSpace())
            {
                return defaultValue;
            }
            try
            {
                return Convert.ToInt32(value);
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 得到对象对应的长整数值
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>得到的长整数值</returns>
        public static long ObjectToLong(object value, long defaultValue = 0L)
        {
            if (value.IsNullOrEmptyOrWhiteSpace())
            {
                return defaultValue;
            }
            try
            {
                return Convert.ToInt64(value);
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 得到对象对应的小数值
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>得到的小数值</returns>
        public static decimal ToDecimal(object value, decimal defaultValue = 0m)
        {
            if (value.IsNullOrEmptyOrWhiteSpace())
            {
                return defaultValue;
            }
            try
            {
                return Convert.ToDecimal(value);
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 得到对象对应的日期值
        /// </summary>
        /// <param name="value">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>得到的日期值</returns>
        public static DateTime? ToDateTime(object value, DateTime? defaultValue = null)
        {
            if (value.IsNullOrEmptyOrWhiteSpace())
            {
                return defaultValue;
            }
            try
            {
                DateTime result = Convert.ToDateTime(value);
                if (result.CompareTo(DateTime.MinValue) == 0)
                {
                    return null;
                }
                else
                {
                    return result;
                }
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 通过反射浅复制
        /// </summary>
        public static object CloneByReflection(object value)
        {
            object result = null;
            if (value != null)
            {
                Type type = value.GetType();
                string typeName = type.FullName;
                if (typeName != null)
                {
                    result = type.Assembly.CreateInstance(typeName);
                    PropertyInfo[] properties = type.GetProperties();
                    foreach (PropertyInfo propertyInfo in properties)
                    {
                        if (propertyInfo.CanRead && propertyInfo.CanWrite)
                        {
                            object propertyValue = propertyInfo.GetValue(value, null);
                            propertyInfo.SetValue(result, propertyValue, null);
                        }
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 对象输出JSON字符串 
        /// </summary>
        public static string ToJson(this object value)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                string result = JsonConvert.SerializeObject(value);
                return result;
            }
        }
    }
}
